package View;

import Controller.UserController;
import pojo.AppData;
import pojo.Customer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UserPersonalInfoWindow extends JFrame{

    private UserController  userController;

    {
        try {
            userController = new UserController();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private JFrame frame = new JFrame("个人信息管理");
    private JButton editButton = new JButton("修改");
    private JButton saveButton = new JButton("保存");
    private JButton backButton = new JButton("返回");

    private JLabel nameLabel = new JLabel("姓名:");
    private JTextField nameTextField = new JTextField(20);
    private JLabel passwordLabel = new JLabel("密码:");
    private JTextField passwordTextField = new JTextField(20);
    private JLabel phoneNumberLabel = new JLabel("电话号码:");
    private JTextField phoneNumberTextField = new JTextField(20);
    private JLabel addressLabel = new JLabel("地址:");
    private JTextField addressTextField = new JTextField(20);
    private JLabel licenseNumberLabel = new JLabel("驾驶证号:");
    private JTextField licenseNumberTextField = new JTextField(20);
    private JLabel balanceLabel = new JLabel("余额:");
    private JTextField balanceTextField = new JTextField(20);
    private JLabel rentalCountLabel = new JLabel("租车数量:");
    private JTextField rentalCountTextField = new JTextField(20);

    public UserPersonalInfoWindow(UserMain_window userMainWindow) {
        frameControl(userMainWindow);
    }

    // 设置容器管理
    public void frameControl(UserMain_window userMainWindow) {
        // 设置底层容器和布局管理器
        frame.setSize(400, 300);
        frame.setLocation(500, 300);
        frame.setResizable(false);

        // 设置信息面板
        JPanel infoPanel = new JPanel();
        infoPanel.setLayout(new GridLayout(7, 2, 10, 10));
        infoPanel.add(nameLabel);
        infoPanel.add(nameTextField);
        infoPanel.add(passwordLabel);
        infoPanel.add(passwordTextField);
        infoPanel.add(phoneNumberLabel);
        infoPanel.add(phoneNumberTextField);
        infoPanel.add(addressLabel);
        infoPanel.add(addressTextField);
        infoPanel.add(licenseNumberLabel);
        infoPanel.add(licenseNumberTextField);
        infoPanel.add(balanceLabel);
        infoPanel.add(balanceTextField);
        infoPanel.add(rentalCountLabel);
        infoPanel.add(rentalCountTextField);

        // 设置按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        buttonPanel.add(editButton);
        buttonPanel.add(saveButton);
        buttonPanel.add(backButton);

        // 设置整体布局
        frame.setLayout(new BorderLayout());
        frame.add(infoPanel, BorderLayout.CENTER);
        frame.add(buttonPanel, BorderLayout.SOUTH);

        // 设置按钮事件处理
        setButtonActions(userMainWindow);

        // 设置窗口可见性和关闭操作
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 初始时显示用户信息
        displayUserInfo();
        // 初始时设置文本框不可编辑
        setFieldsEditable(false);
    }

    // 设置按钮事件处理
    public void setButtonActions(UserMain_window userMainWindow) {
        // 返回编辑按钮事件处理
        editButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 启用编辑
                setFieldsEditable(true);
            }
        });

        // 返回保存修改按钮事件处理
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 保存修改
                setFieldsEditable(false);
                //获取修改的数据
                String username = nameTextField.getText();//姓名
                String password = passwordTextField.getText();//密码
                String phoneNumber = phoneNumberTextField.getText();//电话号码
                String address = addressTextField.getText();//地址
                String licenseNumber = licenseNumberTextField.getText();//驾驶证号码
                int driver_license_number = 0;
                String balance = balanceTextField.getText();//余额
                int left_money = 0;
                String rentalCount = rentalCountTextField.getText();//租车数量
                int rentNumber = 0;

                //转换int
                try {
                    driver_license_number = Integer.parseInt(licenseNumber); // 尝试将驾驶证号码字符串转换为整数
                    left_money = Integer.parseInt(balance); // 尝试将余额字符串转换为整数
                    rentNumber = Integer.parseInt(rentalCount); // 尝试将租车数量字符串转换为整数
                } catch (Exception ex) {
                    // 处理转换失败的情况，比如给出错误消息或者设置默认值
                    System.err.println("前端转换类型错误: " + balance);
                    // 设置默认值
                    driver_license_number = 0;
                    left_money = 0; // 默认值
                    rentNumber = 0; // 默认值
                }

                // 保存修改的逻辑
                // 更新用户信息到数据库
                Customer customer = new Customer();
                customer.setName(username);
                customer.setPassword(password);
                customer.setContact(phoneNumber);
                customer.setAddress(address);
                customer.setDriver_license_number(driver_license_number);
                customer.setLeft_money(left_money);
                customer.setRent_car_number(rentNumber);
                customer.setCustomer_id(AppData.getUserId());
                //更新信息
                Boolean flag = userController.updateUser(customer);
                if (flag){
                    //保存成功的信息
                    JOptionPane.showMessageDialog(UserPersonalInfoWindow.this, "保存成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                }else {
                    //保存失败的信息
                    JOptionPane.showMessageDialog(UserPersonalInfoWindow.this, "保存失败！", "失败", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        // 返回主窗口
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 返回主窗口
                frame.setVisible(false);
                userMainWindow.frame.setVisible(true);
            }
        });
    }

    // 显示用户信息
    private void displayUserInfo() {
        //从数据库查找信息
        Customer customer = null;
        try {
            customer = userController.getCustomerById(AppData.getUserId());
        } catch (Exception e) {
            e.printStackTrace();
        }

        //展示用户信息
        nameTextField.setText(customer.getName());
        passwordTextField.setText(customer.getPassword());
        phoneNumberTextField.setText(customer.getContact());
        addressTextField.setText(customer.getAddress());
        licenseNumberTextField.setText(String.valueOf(customer.getDriver_license_number()));
        balanceTextField.setText(String.valueOf(customer.getLeft_money()));
        rentalCountTextField.setText(String.valueOf(customer.getRent_car_number()));
    }

    // 设置文本框是否可编辑
    private void setFieldsEditable(boolean editable) {
        nameTextField.setEditable(editable);
        passwordTextField.setEditable(editable);
        phoneNumberTextField.setEditable(editable);
        addressTextField.setEditable(editable);
        licenseNumberTextField.setEditable(editable);
        balanceTextField.setEditable(editable);
        rentalCountTextField.setEditable(editable);
    }
}




